import axios from "axios";
import store from "../store/index";
// import "element-ui/lib/theme-chalk/index.css";
// import { Message } from "element-ui";
import { getToken } from "./auth";

// create an axios instance

const service = axios.create({
    baseURL: "http://139.9.249.149:5001/api", // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
});

// request interceptor
service.interceptors.request.use(
    config => {
        // do something before request is sent

        if (store.getters.token || getToken()) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            config.headers["X-Token"] = getToken();
        }
        return config;
    },
    error => {
        // do something with request error
        console.log(error); // for debug
        this.$message({
            type: "error",
            message: "请求失败,请重新登录",
            duration: 2000
        });
        store.dispatch("resetToken").then(() => {
            location.reload();
        });
        return Promise.reject(error);
    }
);

// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        // if the custom code is not 20000, it is judged as an error.
        if (response.status !== 200) {
            this.$message({
                message: response.data.message || "获取信息失败",
                type: "error",
                duration: 3 * 1000
            });

            // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
            if (response.status >= 400) {
                // to re-login
                this.$message.warning({
                    title: "登录验证",
                    message: "登录状态过期或有误,请重新登录"
                });

                store.dispatch("resetToken").then(() => {
                    location.reload();
                });
            }
            return Promise.reject(new Error(response.data.message || "Error"));
        } else {
            return response;
        }
    },
    error => {
        console.log("err" + error); // for debug
        if (!error.response.data.message) {
            this.$message({
                message: "服务器出现问题,请稍后再试",
                type: "error",
                duration: 3 * 1000
            });
        } else {
            this.$message({
                message: error.response.data.message,
                type: "error",
                duration: 3 * 1000
            });
            if (error.response.status === 401) {
                this.$message({
                    message: error.response.data.message,
                    type: "error",
                    duration: 2 * 1000
                });

                store.dispatch("resetToken").then(() => {
                    location.reload();
                });
            }
        }
        return Promise.reject(error);
    }
);

export default service;